package top.oylan.manager.service.impl;

import org.springframework.stereotype.Service;
import top.oylan.manager.entity.Action;
import top.oylan.manager.entity.Comment;
import top.oylan.manager.entity.Liked;
import top.oylan.manager.entity.User;
import top.oylan.manager.entity.example.LikedExample;
import top.oylan.manager.entity.example.UserExample;
import top.oylan.manager.mapper.*;
import top.oylan.manager.service.UserService;
import top.oylan.manager.tools.Pager;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created by A on 2019/2/11.
 */
@Service("userService")
public class UserServiceImpl implements UserService {

    @Resource
    UserMapper mapper;

    @Resource
    ActionMapper actionMapper;

    @Override
    public User selectUserById(Integer id) {
       return mapper.selectByPrimaryKey(id);
    }

    @Override
    public User selectUserByAccount(String account) {

        return mapper.selectByAccount(account);
    }

    @Override
    public List<User> selectManagerByPager(Pager<User> pager) {
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();

        if(pager.getCondition().getType() != null){
            criteria.andTypeEqualTo(pager.getCondition().getType());
        }

        if(pager.getCondition().getStatus() != null){
            criteria.andStatusEqualTo(pager.getCondition().getStatus());
        }

        long num = mapper.countByExample(example);
        pager.setTotalRow((int) num);

        example.setRowSize(pager.getEveyPageRow());
        example.setOffset((pager.getCurrentPage()-1)*pager.getEveyPageRow());

        return mapper.selectUserByPager(example);
    }

    @Override
    public List<Action> findActionsByUserId(Integer id) {
        return actionMapper.findActionsByUserId(id);
    }

    @Override
    public void add(User user) {
        mapper.insertSelective(user);
    }

    @Resource
    RoleMapper roleMapper;

    @Override
    public void addManger(User user, List<Integer> idsList) {
        user.setCreateTime(new Date());
        user.setType((byte)1);
        this.add(user);
        int userId = user.getId();
        if(userId > 0){
            roleMapper.saveUserRoles(userId,idsList);
        }
    }

    @Override
    public void updateManger(User user, List<Integer> idsList) {
        mapper.updateByPrimaryKeySelective(user);
        roleMapper.deleteUserRoles(user.getId());
        roleMapper.saveUserRoles(user.getId(),idsList);
    }

    @Override
    public User findUserByPhone(String phone) {
        return mapper.findUserByPhone(phone);
    }

    @Override
    public int updateUser(User user) {
        return mapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public int addUser(User user) {
        return mapper.insertSelective(user);
    }

    @Resource
    CommentMapper commentMapper;

    @Override
    public int comment(Comment comment) {
        return commentMapper.insertSelective(comment);
    }

    @Resource
    LikedMapper likedMapper;

    @Override
    public int saveLiked(Liked liked) {
        LikedExample example = getExample(liked);
        List<Liked> likeds = likedMapper.selectByExample(example);
        if(likeds.size() > 0){
            liked.setId(likeds.get(0).getId());
            liked.setStatus((byte)1);
            int set = likedMapper.updateByIdSelective(liked);
            updateLikedNum(liked,set,1);
            return 1;
        }else{
            int set = likedMapper.insertSelective(liked);
            updateLikedNum(liked,set,1);
            return set;
        }
    }

    @Override
    public int cancelLike(Liked liked) {
         LikedExample example = getExample(liked);
         liked.setStatus((byte)0);
         int set = likedMapper.updateByExampleSelective (liked,example);
         updateLikedNum(liked,set,0);
         return set;
    }

    private LikedExample getExample(Liked liked) {
        LikedExample example = new LikedExample();
        LikedExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(liked.getUserId());
        criteria.andItemIdEqualTo(liked.getItemId());
        criteria.andTypeEqualTo(liked.getType());
        return example;
    }

    @Resource
    ProductMapper productMapper;

    void updateLikedNum(Liked liked,int set,int inc){
        if(set > 0){
            if(liked.getType()==1){
                productMapper.creaseLiked(liked.getItemId(),inc);
            }else if(liked.getType()==3){
                commentMapper.creaseLiked(liked.getItemId(),inc);
            }
        }
    }
}
